package com.example.lin.easypermission_demo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //将 权申请的回调函数 转交由 EasyPermissions 处理
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    private void call(String phone) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phone));
        startActivity(intent);
    }

    int REQUEST_CODE = 111;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        //内部已经做了版本判断
        boolean permissions = EasyPermissions.hasPermissions(this, Manifest.permission.CALL_PHONE);
        if (!permissions) {
            //请求权限
            EasyPermissions.requestPermissions(this, "App需要使用打电话权限,才可以运行啊.否则怎么抠你的费用啊", REQUEST_CODE, Manifest.permission.CALL_PHONE);
        } else {
            call("13843838438");
        }
    }

    /**
     * 接受 被授予的权限
     *
     * @param requestCode 请求码
     * @param perms       权限列表
     */
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        call("13843838438");
    }

    /**
     * 接受 被拒绝的权限
     *
     * @param requestCode 请求码
     * @param perms       权限列表
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        //是否有一些权限被永久拒绝
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            //引导用户到 设置界面 手动开启权限
            new AppSettingsDialog.Builder(this)
                    //弹出框 标题
                    .setTitle("权限申请")
                    //弹出框的内容
                    .setRationale("App需要使用打电话权限,是否前往设置界面手动打开")
                    .build()
                    .show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
//            从弹出框返回到该Activity
            case AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE:
                if (resultCode == Activity.RESULT_CANCELED) {
                    Toast.makeText(this, "引导用户去启动App的结果的回调", Toast.LENGTH_SHORT).show();
                    //TODO 判断 用户是否启动了权限,然后搞事情....
//                    boolean permissions = EasyPermissions.hasPermissions(this, Manifest.permission.CALL_PHONE);
//                    if (!permissions)
//                        finish();
                }
                break;
        }
    }
}
